package cn.iocoder.yudao.module.crossborder.platform.service.platformproductmapping;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformproductmapping.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformproductmapping.PlatformProductMappingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformproductmapping.PlatformProductMappingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商平台产品映射 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformProductMappingServiceImpl implements PlatformProductMappingService {

    @Resource
    private PlatformProductMappingMapper platformProductMappingMapper;

    @Override
    public Long createPlatformProductMapping(PlatformProductMappingSaveReqVO createReqVO) {
        // 插入
        PlatformProductMappingDO platformProductMapping = BeanUtils.toBean(createReqVO, PlatformProductMappingDO.class);
        platformProductMappingMapper.insert(platformProductMapping);

        // 返回
        return platformProductMapping.getId();
    }

    @Override
    public void updatePlatformProductMapping(PlatformProductMappingSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformProductMappingExists(updateReqVO.getId());
        // 更新
        PlatformProductMappingDO updateObj = BeanUtils.toBean(updateReqVO, PlatformProductMappingDO.class);
        platformProductMappingMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformProductMapping(Long id) {
        // 校验存在
        validatePlatformProductMappingExists(id);
        // 删除
        platformProductMappingMapper.deleteById(id);
    }

    @Override
        public void deletePlatformProductMappingListByIds(List<Long> ids) {
        // 删除
        platformProductMappingMapper.deleteByIds(ids);
        }


    private void validatePlatformProductMappingExists(Long id) {
        if (platformProductMappingMapper.selectById(id) == null) {
            throw exception(PLATFORM_PRODUCT_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public PlatformProductMappingDO getPlatformProductMapping(Long id) {
        return platformProductMappingMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformProductMappingDO> getPlatformProductMappingPage(PlatformProductMappingPageReqVO pageReqVO) {
        return platformProductMappingMapper.selectPage(pageReqVO);
    }

}